//
//  MyChartingViewController.m
//  intelligent
//
//  Created by 甘世清 on 2024/3/1.
//

#import "MyChartingViewController.h"
#import "MyChartingTopView.h"
#import "MyChartingCollectionViewCell.h"
#import "MyChartingModel.h"
#import "DrawingWaitViewController.h"
#import "DeletedMyChartingView.h"
@interface MyChartingViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic , strong)MyChartingTopView * topView;
@property (nonatomic , strong) UIButton * deletedBtn;
@property (nonatomic ) BOOL isdeleted;
@end

@implementation MyChartingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.zx_navTitle = SQNSLocalString(@"我的图片薄");
    WS(weakSelf);
    [self zx_setRightBtnWithImg:SQGetImage(@"删除") clickedBlock:^(ZXNavItemBtn * _Nonnull btn) {
        weakSelf.isdeleted = !weakSelf.isdeleted;
        weakSelf.deletedBtn.hidden = !weakSelf.isdeleted;
        [weakSelf.collectionView reloadData];
    }];
}

- (void)SQ_UIViewForSelfView{
    [self.view addSubview:self.topView];
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(50);
        make.top.equalTo(self.zx_navBar.mas_bottom);
    }];
    WS(weakSelf);
    [self.topView setSeletedBlock:^(HotModel * _Nonnull model) {
        weakSelf.isdeleted = NO;
        weakSelf.deletedBtn.hidden = !weakSelf.isdeleted;
        [weakSelf.collectionView.mj_header beginRefreshing];
    }];
    self.topView.jsonArray = [[NSMutableArray alloc]initWithArray:self.topArray];
    [self.collectionView registerClass:[MyChartingCollectionViewCell class] forCellWithReuseIdentifier:@"MyChartingCollectionViewCell"];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.view addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self.view);
            make.top.equalTo(self.topView.mas_bottom);
    }];
    [self.view addSubview:self.deletedBtn];
    [self.deletedBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.width.mas_equalTo(SCREEN_WIDETH-50);
        make.height.mas_equalTo(48);
        make.bottom.equalTo(self.view.mas_bottom).offset(-FootHeight-40);
    }];
    [self SQ_collectViewAddRefresh];
    [self SQ_Header];
}
- (void)SQ_Header{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    if (self.topView.seletedModel) {
        [dic setObject:self.topView.seletedModel.userid forKey:@"sizeId"];
    }
    [dic setObject:@"1" forKey:@"current"];
    [dic setObject:@"20" forKey:@"size"];
    [HttpTool post:@"img/imgList" paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        NSDictionary * data = [[NSDictionary alloc]initWithDictionary:httpModel.data];
        NSArray * array = [MyChartingModel mj_objectArrayWithKeyValuesArray:[data objectForKey:@"records"]];
        [self.jsonArray removeAllObjects];
        [self.jsonArray addObjectsFromArray:array];
        [self.collectionView.mj_header endRefreshing];
        [self.collectionView reloadData];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [self.collectionView.mj_header endRefreshing];
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
}
- (void)SQ_Foother{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:[NSString stringWithFormat:@"%d",self.page+1] forKey:@"current"];
    [dic setObject:@"20" forKey:@"size"];
    if (self.topView.seletedModel) {
        [dic setObject:self.topView.seletedModel.userid forKey:@"sizeId"];
    }
    [HttpTool post:@"img/imgList" paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        self.page ++;
        NSDictionary * data = [[NSDictionary alloc]initWithDictionary:httpModel.data];
        NSArray * array = [MyChartingModel mj_objectArrayWithKeyValuesArray:[data objectForKey:@"records"]];
        [self.jsonArray addObjectsFromArray:array];
        [self.collectionView reloadData];
        [self.collectionView.mj_footer endRefreshing];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [self.collectionView.mj_footer endRefreshing];
    }];
}
#pragma mark UICollectionViewDelegate,UICollectionViewDataSource
 
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.jsonArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath{
    
    MyChartingCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyChartingCollectionViewCell" forIndexPath:indexPath];
    MyChartingModel * model = [self.jsonArray objectAtIndex:indexPath.item];
    cell.model = model;
    cell.rightBtn.hidden = !self.isdeleted;
    [cell.rightBtn setTouchBlock:^(SQCustomButton * _Nonnull button) {
        model.deleteFlag = !model.deleteFlag;
        if (model.deleteFlag) {
            button.image = SQGetImage(@"选中1");
        } else {
            button.image = SQGetImage(@"未选中");
        }
    }];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
   
    return self.topView.seletedModel.cellSize;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
   
    return 8;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 15, 0, 15);
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    DrawingWaitViewController * vc = [[DrawingWaitViewController alloc]init];
    vc.model = [self.jsonArray objectAtIndex:indexPath.item];
    [self SQ_pushViewControllerFull:vc];
}
-(void)SQ_deleteButtonClick{
    NSMutableArray * array= [[NSMutableArray alloc]init];
    NSMutableArray * arraynew = [[NSMutableArray alloc]init];
    for (MyChartingModel * model in self.jsonArray) {
        if (model.deleteFlag) {
            [array addObject:model.userid];
        }else{
            [arraynew addObject:model];
        }
    }
    if (array.count<=0) {
        [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"请选择")];
        return;
    }
    [self SQ_ShowDeleteView:array towArray:arraynew];
   
}
-(void)SQ_ShowDeleteView:(NSArray*)array towArray:(NSArray*)arraynew{
    DeletedMyChartingView * view = [[DeletedMyChartingView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDETH, SCREEN_HEIGHT)];
    LSTPopView * pop = [LSTPopView initWithCustomView:view
                                           parentView:nil
                                             popStyle:LSTPopStyleSmoothFromBottom dismissStyle:LSTDismissStyleSmoothToBottom];
    pop.hemStyle = LSTHemStyleCenter;
    [pop pop];
    LSTPopViewWK(pop)
    [view.leftBtn setTouchBlock:^(SQCustomButton * _Nonnull button) {
        [wk_pop dismiss];
    }];
    [view.rightBtn setTouchBlock:^(SQCustomButton * _Nonnull button) {
        NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
        [dic setObject:[array componentsJoinedByString:@","] forKey:@"ids"];
        [HttpTool post:@"img/deleteImg" paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
            [wk_pop dismiss];
            [self.jsonArray removeAllObjects];
            [self.jsonArray addObjectsFromArray:arraynew];
            [self.collectionView reloadData];
        } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
            [YJProgressHUD showMsgWithoutView:httpModel.message];
        }];
    }];
   
}
- (MyChartingTopView *)topView{
    if (!_topView) {
        _topView = [[MyChartingTopView alloc]init];
        
    }
    return _topView;
}
- (UIButton *)deletedBtn{
    if (!_deletedBtn) {
        _deletedBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _deletedBtn.hidden = YES;
        _deletedBtn.backgroundColor = SQColor(@"#4AF8CE");
        [_deletedBtn setTitle:SQNSLocalString(@"确认删除") forState:normal];
        [_deletedBtn setTitleColor:SQColor333333 forState:normal];
        _deletedBtn.layer.cornerRadius = 6;
        _deletedBtn.layer.masksToBounds = YES;
        [_deletedBtn addTarget:self action:@selector(SQ_deleteButtonClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _deletedBtn;
}
@end
